<?php
/**
 * CLI use:
 * php util.php thecommand arguments
 * 
 * Broswer use:
 * http://server.com/path/util.php?command=thecommand&args=arguments
 * 
 * "thecommand" is the operation to run.
 * "arguments" is a list of arguments seperated by spaces.
 * 
 * The first argument is always the command to execute.
 */

require_once('../../autoload.php');

//If arguments are set by the browser, use them like the CLI arguments
if ( isset( $_REQUEST['command'] ) )
{
	$argv =  array('util.php', $_REQUEST['command'] );
	if ( isset( $_REQUEST['args'] ) )
	{
		$argv = array_merge( $argv, explode( ' ', $_REQUEST['args'] ) );
	}
	$argc = count( $argv );
}

//If no arguments are given or help is requested, display usage information
if ( $argc < 1 || in_array( $argv[1], array('-h', '--help', '/?', 'help' ) ) )
{
	display_usage();
}

//Seperate the arguments and the command
$command = $argv[1];
$arguments = array();
for ( $i = 2 ; $i < count( $argv ) ; $i++ )
{
	$arguments[] = $argv[$i];
}

//Run the requested command
switch ( $command )
{
	case "xmldump":
		
		$server = ( isset( $arguments[0] ) ) ? $arguments[0] : null;
		$user = ( isset( $arguments[1] ) ) ? $arguments[1] : null;
		$pass = ( isset( $arguments[2] ) ) ? $arguments[2] : null;
		
		$dom = db_dump_dom( $server, $user, $pass );
		echo $dom->saveXML();
		break;
		
	case "xsldump":
		$file = ( isset( $arguments[0] ) ) ? $arguments[0] : null;
		$server = ( isset( $arguments[1] ) ) ? $arguments[1] : null;
		$user = ( isset( $arguments[2] ) ) ? $arguments[2] : null;
		$pass = ( isset( $arguments[3] ) ) ? $arguments[3] : null;
		
		$dom = db_dump_dom( $server, $user, $pass );
		
		$xsl = new DOMDocument;
		$xsl->load($file);
		
		$proc = new XSLTProcessor;
		$proc->registerPHPFunctions();
		$proc->importStyleSheet($xsl);
		
		echo "<?php\n" . $proc->transformToXml($dom) . "?>\n";
		break;
	
	case "name":
		$string = implode( ' ', $arguments );
		echo camel_case( $string ) ."\n";
		break;
		
	default:
		echo "Unknown command\n";
		display_usage();
}

function display_usage() {
	global $argv;
	echo "Usage:
	".$argv[0]." command <arguments>

	Commands:
		help : prints this menu
		xmldump <server> <user> <password> : Creates an XML dump of all database information
		xsldump <stylesheet> <user> <password> : Styles xmldump with an XSL stylesheet
		name <class_name> : Create a camel-case class name from a string\n";
	exit();
}

function db_dump_dom( $server, $user, $pass )
{
	$connection = new OORDBMySQLConnection( $server, $user, $pass );
	$dom = new DOMDocument();
	$dom->formatOutput = true;
	$databasesNode = $dom->appendChild( $dom->createElement( 'databases' ) );
	$databasesNode->setAttribute( 'user', $user );
	$databasesNode->setAttribute( 'server', $server );
	foreach ( $connection as $database )
	{
		$databaseNode = $databasesNode->appendChild( $dom->createElement( 'database' ) );
		$databaseNode->setAttribute( 'name' , $database->getName() );
		foreach ( $database as $table )
		{
			$tableNode = $databaseNode->appendChild( $dom->createElement( 'table' ) );
			$tableNode->setAttribute( 'name' , $table->getName() );
			$rowsNode = $tableNode->appendChild( $dom->createElement( 'rows' ) );
			foreach ( $table as $key => $row )
			{
				$rowNode = $rowsNode->appendChild( $dom->createElement( 'row' ) );
				foreach ( $row as $field => $value )
				{
					
					$fieldNode = $rowNode->appendChild( $dom->createElement( 'field' ) );
					$fieldNode->setAttribute( 'name' , $field );
					$fieldNode->appendChild( $dom->createCDATASection( $value ) );
				}
			}
			$fieldsNode = $tableNode->appendChild( $dom->createElement( 'fields' ) );
			foreach ( $table->getFields() as $field )
			{
				$fieldNode = $fieldsNode->appendChild( $dom->createElement( 'field' ) );
				$fieldNode->setAttribute( 'name' , $field );				
			}
		}
	}
	return $dom;
}

function camel_case( $string )
{
	$new = strtoupper( substr( $string, 0, 1 ) );
	$makeCapital = false;
	for ( $i = 1 ; $i < strlen( $string ) ; $i++ )
	{
		$letter = substr( $string, $i, 1 );
		if ( preg_match( '/[A-Za-z0-9]/', $letter ) == 1 )
		{
			if ( $makeCapital )
			{
				$letter = strtoupper( $letter );
				$makeCapital = false;
			}
			$new .= $letter;
		}
		elseif ( preg_match( '/[ _]/', $letter ) == 1 )
		{
			$makeCapital = true;
		}
	}
	return $new;
}

function camel_case_lower_first( $string )
{
	$new = strtolower( substr( $string, 0, 1 ) );
	$makeCapital = false;
	for ( $i = 1 ; $i < strlen( $string ) ; $i++ )
	{
		$letter = substr( $string, $i, 1 );
		if ( preg_match( '/[A-Za-z0-9]/', $letter ) == 1 )
		{
			if ( $makeCapital )
			{
				$letter = strtoupper( $letter );
				$makeCapital = false;
			}
			$new .= $letter;
		}
		elseif ( preg_match( '/[ _]/', $letter ) == 1 )
		{
			$makeCapital = true;
		}
	}
	return $new;
}
?>
